package org.bbs.demo.utils.config;

import org.bbs.demo.utils.jwtinterepter.JWTinterepter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Override
    // 跨域配置
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowedOriginPatterns("*")
//                .allowedOrigins("http://localhost:8080")
                .allowCredentials(true); // 服务器设置true的时候，浏览器必须设置withCredentials: true
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(jwtInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/Hello")
                .excludePathPatterns("/test1")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/signup");
    }


    @Bean
    public JWTinterepter jwtInterceptor() {
        return new JWTinterepter();
    }
}
